QuickOPC User's Guide and Reference
Examples - OPC Alarms&Events - Callback using lambda or regular method
// This example shows how to subscribe to events and display the event message with each notification, using a callback method
// specified using lambda expression.

using System;
using System.Diagnostics;
using System.Threading;
using OpcLabs.EasyOpc.AlarmsAndEvents;

namespace DocExamples.AlarmsAndEvents._EasyAEClient
{
    partial class SubscribeEvents
    {
        public static void CallbackLambda()
        {
            // Instantiate the client object.
            var client = new EasyAEClient();

            Console.WriteLine("Subscribing...");
            // The callback is a lambda expression the displays the event message
            client.SubscribeEvents("", "OPCLabs.KitEventServer.2", 1000,
                (sender, eventArgs) =>
                {
                    Debug.Assert(eventArgs != null);
                    if (!eventArgs.Succeeded)
                    {
                        Console.WriteLine("*** Failure: {0}", eventArgs.ErrorMessageBrief);
                        return;
                    }
                    if (!(eventArgs.EventData is null))
                        Console.WriteLine(eventArgs.EventData.Message);
                });

            Console.WriteLine("Processing event notifications for 20 seconds...");
            Thread.Sleep(20 * 1000);

            Console.WriteLine("Unsubscribing...");
            client.UnsubscribeAllEvents();

            Console.WriteLine("Waiting for 2 seconds...");
            Thread.Sleep(2 * 1000);
        }
    }
}
# This example shows how to subscribe to events and display the event message with each notification, using a regular
# callback method.

# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc
import time

# Import .NET namespaces.
from OpcLabs.EasyOpc import *
from OpcLabs.EasyOpc.AlarmsAndEvents import *


# Notification callback.
def callback(sender, e):
    assert e is not None
    if not e.Succeeded:
        print('*** Failure: ', e.ErrorMessageBrief, sep='')
        return
    else:
        if e.EventData is not None:
            print(e.EventData.Message)


# Instantiate the client object
client = EasyAEClient()

print('Subscribing events...')
# The callback is a regular method that  displays the event message.
handle = IEasyAEClientExtension.SubscribeEvents(client,
                                                '', 'OPCLabs.KitEventServer.2', 1000,
                                                EasyAENotificationEventHandler(callback))

print('Processing event notifications for 20 seconds...')
time.sleep(20)

print('Unsubscribing events...')
client.UnsubscribeAllEvents()

print('Waiting for 2 seconds...')
time.sleep(2)

print('Finished.')
' This example shows how to subscribe to events and display the event message with each notification, using a callback method
' specified using lambda expression.

Imports OpcLabs.EasyOpc.AlarmsAndEvents

Namespace AlarmsAndEvents._EasyAEClient
    Partial Friend Class SubscribeEvents
        Public Shared Sub CallbackLambda()
            ' Instantiate the client object
            Dim client = New EasyAEClient()

            Console.WriteLine("Subscribing...")
            ' The callback is a lambda expression the displays the event message
            client.SubscribeEvents("", "OPCLabs.KitEventServer.2", 1000,
                    Sub(sender, eventArgs)
                        Debug.Assert(eventArgs IsNot Nothing)
                        If Not eventArgs.Succeeded Then
                            Console.WriteLine("*** Failure: {0}", eventArgs.ErrorMessageBrief)
                            Exit Sub
                        End If
                        If eventArgs.EventData IsNot Nothing Then
                            Console.WriteLine(eventArgs.EventData.Message)
                        End If
                    End Sub)

            Console.WriteLine("Processing event notifications for 20 seconds...")
            Threading.Thread.Sleep(20 * 1000)

            Console.WriteLine("Unsubscribing...")
            client.UnsubscribeAllEvents()

            Console.WriteLine("Waiting for 2 seconds...")
            Threading.Thread.Sleep(2 * 1000)
        End Sub
    End Class
End Namespace

 

See Also

Conceptual

Examples - OPC UA Alarms&Conditions

Examples - Reactive Programming